﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.IO.IsolatedStorage;
using System.Globalization;
using System.IO;

namespace TFSAutoShelve
{
    /// <summary>
    /// An application that automatically shelves all pending changes.
    /// </summary>
    class Program
    {
        /// <summary>
        /// The application entry point.
        /// </summary>
        /// <param name="args">The args.</param>
        static void Main(string[] args)
        {
            TextWriterTraceListener logTL = null;
            ConsoleTraceListener consoleTL = null;
            IsolatedStorageFileStream isoStream = null;

            try
            {
                // Store the trace log file in isolated storage
                //  as some users will not have permission to write
                //  to the directory this program is running from (ex. log off scripts directory)

                // Get the isolated store
                IsolatedStorageFile isoStore =
                    IsolatedStorageFile.GetUserStoreForAssembly();

                // Create a file stream for the log file
                isoStream = new IsolatedStorageFileStream(
                    Properties.Resources.LOG_FILE,
                    FileMode.Create,
                    isoStore);

                // Create a listener to direct trace out to a log file
                logTL = new TextWriterTraceListener(isoStream);
                Trace.Listeners.Add(logTL);

                // Create a listener to direct trace out to the console
                consoleTL = new ConsoleTraceListener();
                Trace.Listeners.Add(consoleTL);
                
                // Shelve pending changes
                AutoShelve.Shelve(
                    Properties.Settings.Default.TFSServer,
                    Properties.Settings.Default.ShelveSetName);
 
            }
            catch (Exception ex)
            {
                Trace.WriteLine(ex);

                try
                {
                    // Send error report to current user
                    ErrorReport.SendErrorReport(
                        ex,
                        Properties.Settings.Default.ErrorReportFromAddress,
                        ADHelper.GetUserEmailAddress(),
                        Properties.Settings.Default.ErrorReportCCAddressList,
                        Properties.Settings.Default.SmtpHostName);
                }
                catch (Exception exNotif)
                {
                    Trace.WriteLine(
                        string.Format(
                        CultureInfo.InvariantCulture,
                        Properties.Resources.ERR_NOTIFICATION_FAILED,
                        exNotif.Message));

                    Trace.WriteLine(ErrorReport.FormatException(exNotif));
                }
            }
            finally
            {
                // Close console trace listener
                if (consoleTL != null)
                {
                    consoleTL.Flush();
                    consoleTL.Close();
                }

                // Close log file trace listener
                if (logTL != null)
                {
                    logTL.Flush();
                    logTL.Close();
                }

            }
      
        }
    }
}
